package com.groupbuying.store.service.impl;

import com.groupbuying.store.constants.CommonConstants;
import com.groupbuying.store.constants.StoreMainInfoEnumConstants;
import com.groupbuying.store.context.FilterContextHandler;
import com.groupbuying.store.connector.CurrencyFeign;
import com.groupbuying.store.connector.SysconfigFeign;
import com.groupbuying.store.dao.StoreCheckoutCounterDao;
import com.groupbuying.store.dao.StoreMainInfoDao;
import com.groupbuying.store.domain.StoreCheckoutCounterDO;
import com.groupbuying.store.domain.StoreMainInfoDO;
import com.groupbuying.store.domain.StoreOpexRuleRelDO;
import com.groupbuying.store.domain.StoreShopDO;
import com.groupbuying.store.dto.forExternal.StoreCounterInfoDTO;
import com.groupbuying.store.pojo.StoreMainInfoEnum;
import com.groupbuying.store.service.*;
import com.groupbuying.store.utils.apiresult.ApiResult;
import com.groupbuying.store.utils.apiresult.ApiResultCode;
import org.apache.commons.lang3.StringUtils;
import org.reflections.Store;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @anthor wenguoqiang
 * @date: 2019/01/24 10:18
 * @company 够样
 * @des
 **/
@Service
@Transactional
public class StoreCounterApiServiceImpl implements StoreCounterApiService {
    private static final Logger logger = LoggerFactory.getLogger(StoreCounterApiServiceImpl.class);

    @Autowired
    StoreCheckoutCounterService storeCheckoutCounterService;

    @Autowired
    StoreShopService storeShopService;

    @Autowired
    CurrencyFeign currencyFeign;

    @Autowired
    StoreOpexRuleRelService storeOpexRuleRelService;

    @Autowired
    SysconfigFeign sysconfigFeign;

    @Autowired
    StoreCheckoutCounterDao storeCheckoutCounterDao;

    @Autowired
    StoreMainInfoService storeMainInfoService;


    @Override
    public ApiResult getCurrencyCheckoutCounterInfo(Map<String, Object> mapParams) {
        // 解析map参数
        Object tempParam = mapParams.get("counterUuid");
        String counterUuid = tempParam == null ? null : tempParam.toString();

        // 如果获取手机号或密码为空，则返回false
        if (StringUtils.isEmpty(counterUuid)) {
            return ApiResult.paramIsNull();
        }

        StoreCounterInfoDTO storeCounterInfoDTO = new StoreCounterInfoDTO();
        //收银台id
        storeCounterInfoDTO.setCounterId(counterUuid);
        StoreCheckoutCounterDO storeCheckoutCounterDO = storeCheckoutCounterService.get(counterUuid);

        if (storeCheckoutCounterDO == null) {
            return ApiResult.failure(ApiResultCode.DATA_INVALID, "收银台为空");
        }

        StoreMainInfoDO storeMainInfoDO = storeMainInfoService.get(storeCheckoutCounterDO.getStoreuuid());

        //Todo: 财务审核功能
        if (storeCheckoutCounterDO.getDelflag().equals(CommonConstants.COMMON_FLAG_DEL)
                || storeMainInfoDO == null || storeMainInfoDO.getDelflag().equals(CommonConstants.COMMON_FLAG_DEL)
                || !(StoreMainInfoEnum.BUSINESS_AUDIT_PASS.equals(storeMainInfoDO.getAuditstatus()))
                || StoreMainInfoEnum.FROZEN.equals(storeMainInfoDO.getFreezestatus()) || StoreMainInfoEnumConstants.ENUM_STORE_TYPE_GYYHMD.equals(storeMainInfoDO.getStoretype())
                ) {
            return ApiResult.failure(ApiResultCode.DATA_INVALID, "收银台无效");
        }

        //StoreShopDO storeShopDO = storeShopService.getByStoreUuid(storeCheckoutCounterDO.getStoreuuid());

        //店名
        storeCounterInfoDTO.setShopName(storeMainInfoDO.getStorename());

        Map searchMap = new HashMap();
        Calendar now = Calendar.getInstance();
        boolean isFirstSunday = (now.getFirstDayOfWeek() == Calendar.SUNDAY);
        int weekDay = now.get(Calendar.DAY_OF_WEEK);
        if (isFirstSunday) {
            weekDay = weekDay - 1;
            if (weekDay == 0) {
                weekDay = 7;
            }
        }

        //商户积分规则
        String ruleId;
        searchMap.put("dayId", weekDay);
        searchMap.put("delFlag", CommonConstants.COMMON_FLAG_UNDEL);
        searchMap.put("storeId", storeMainInfoDO.getUuid());
        List<StoreOpexRuleRelDO> list_opex = storeOpexRuleRelService.list(searchMap);
        if (list_opex == null || list_opex.size() == 0) {
            //加载默认规则
            //无规则不能使用
            //测试期间使用默认规则
//            Map nowMap = new HashMap();
//            nowMap.put("dayId", "1");
//            nowMap.put("delFlag", CommonConstants.COMMON_FLAG_UNDEL);
//            nowMap.put("storeId", storeShopDO.getStoreuuid());
//            List<StoreOpexRuleRelDO> now_list_opex = storeOpexRuleRelService.list(nowMap);
//
//            StoreOpexRuleRelDO storeOpexRuleRelDO = now_list_opex.get(0);
//            ruleId = storeOpexRuleRelDO.getRuleId();

            //新版本
            return ApiResult.failure(ApiResultCode.DATA_INVALID, "收银台无效");
        } else {
            //加载该天规则
            StoreOpexRuleRelDO storeOpexRuleRelDO = list_opex.get(0);
            ruleId = storeOpexRuleRelDO.getRuleId();
        }

        Map ruleMap = new HashMap();
        ruleMap.put("ruleId", ruleId);
        ruleMap.put("delFlag", CommonConstants.COMMON_FLAG_UNDEL);
        Map ruleRes = sysconfigFeign.getStoreOpexRule(ruleMap);
        if (ruleRes != null) {
            BigDecimal backCurrency = new BigDecimal(ruleRes.get("backCurrencyRatio").toString());
            //backCurrency = backCurrency.multiply(new BigDecimal(100));
            storeCounterInfoDTO.setRatio(backCurrency);
            BigDecimal discount = new BigDecimal(ruleRes.get("currencyPayDiscount").toString());
            discount = discount.divide(new BigDecimal(10));
            storeCounterInfoDTO.setDiscount(discount);
            storeCounterInfoDTO.setOpxRuleId(ruleId);
        } else {
            //无规则不能使用
            return ApiResult.failure(ApiResultCode.DATA_INVALID, "收银台无效");
        }
        return ApiResult.success(storeCounterInfoDTO);
    }


    @Override
    public ApiResult getCheckoutCounterInfo(Map<String, Object> mapParams) {
        // 解析map参数
        Object tempParam = mapParams.get("counterUuid");
        String counterUuid = tempParam == null ? null : tempParam.toString();

        if (StringUtils.isEmpty(counterUuid)) {
            return ApiResult.paramIsNull();
        }

        StoreCounterInfoDTO storeCounterInfoDTO = new StoreCounterInfoDTO();
        //收银台id
        storeCounterInfoDTO.setCounterId(counterUuid);

        StoreCheckoutCounterDO storeCheckoutCounterDO = storeCheckoutCounterService.get(counterUuid);

        if (storeCheckoutCounterDO == null) {
            return ApiResult.failure(ApiResultCode.DATA_INVALID, "收银台为空");
        }

        StoreMainInfoDO storeMainInfoDO = storeMainInfoService.get(storeCheckoutCounterDO.getStoreuuid());

        //Todo: 财务审核功能
        if (storeCheckoutCounterDO.getDelflag().equals(CommonConstants.COMMON_FLAG_DEL)
                || storeMainInfoDO == null || storeMainInfoDO.getDelflag().equals(CommonConstants.COMMON_FLAG_DEL)
                || !(StoreMainInfoEnum.BUSINESS_AUDIT_PASS.equals(storeMainInfoDO.getAuditstatus()))
                || StoreMainInfoEnum.FROZEN.equals(storeMainInfoDO.getFreezestatus()) || StoreMainInfoEnumConstants.ENUM_STORE_TYPE_GYYD.equals(storeMainInfoDO.getStoretype())
                ) {
            return ApiResult.failure(ApiResultCode.DATA_INVALID, "收银台无效");
        }

        //StoreShopDO storeShopDO = storeShopService.getByStoreUuid(storeCheckoutCounterDO.getStoreuuid());
        storeCounterInfoDTO.setIsMobileVerify(storeMainInfoDO.getRecommendman());
        //店名
        storeCounterInfoDTO.setShopName(storeMainInfoDO.getStorename());
        return ApiResult.success(storeCounterInfoDTO);
    }

    @Override
    public String getCounterNameByCustomerUuid(String customerUuid) {
        StoreCheckoutCounterDO storeCheckoutCounterDO = storeCheckoutCounterDao.get(customerUuid);
        if (storeCheckoutCounterDO == null || StringUtils.isBlank(storeCheckoutCounterDO.getCountername())) {
            return "未知";
        }

        return storeCheckoutCounterDO.getCountername();
    }

    @Override
    public ApiResult checkCounter(Map<String, Object> params) {
        // 解析map参数
        Object tempParam = params.get("counterUuid");
        String counterUuid = tempParam == null ? null : tempParam.toString();

        StoreCheckoutCounterDO storeCheckoutCounterDO = storeCheckoutCounterService.get(counterUuid);

        if (storeCheckoutCounterDO == null) {
            return ApiResult.failure(ApiResultCode.DATA_INVALID, "收银台不存在");
        }

        StoreMainInfoDO storeMainInfoDO = storeMainInfoService.get(storeCheckoutCounterDO.getStoreuuid());

        //Todo: 财务审核功能
        if (storeCheckoutCounterDO.getDelflag().equals(CommonConstants.COMMON_FLAG_DEL)
                || storeMainInfoDO == null || storeMainInfoDO.getDelflag().equals(CommonConstants.COMMON_FLAG_DEL)
                || !(StoreMainInfoEnum.BUSINESS_AUDIT_PASS.equals(storeMainInfoDO.getAuditstatus()))
                || StoreMainInfoEnum.FROZEN.equals(storeMainInfoDO.getFreezestatus())
                ) {
            return ApiResult.failure(ApiResultCode.DATA_INVALID, "收银台不存在");
        }
        return ApiResult.success();
    }
}
